package com.aidex.biz.controller;

import com.aidex.biz.domain.JinjianKpi;
import com.aidex.biz.service.JinjianKpiService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工业绩Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/biz/bizJinjianKpi")
public class JinjianKpiController extends BaseController {
    @Autowired
    private JinjianKpiService jinjianKpiService;

    /**
     * 查询员工业绩列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(JinjianKpi jinjianKpi, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            jinjianKpi.setCompanyId(null);
        } else {
            jinjianKpi.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        jinjianKpi.setPage(new PageDomain(request, response));
        return R.data(jinjianKpiService.findPage(jinjianKpi));
    }

    /**
     * 获取员工业绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianKpi:query')")
    @GetMapping(value = "/{id}")
    public R<JinjianKpi> detail(@PathVariable("id") String id) {
        return R.data(jinjianKpiService.get(id));
    }

    /**
     * 新增员工业绩
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianKpi:add')")
    @Log(title = "员工业绩", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated JinjianKpi jinjianKpi) {
        return R.status(jinjianKpiService.save(jinjianKpi));
    }

    /**
     * 修改员工业绩
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianKpi:edit')")
    @Log(title = "员工业绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated JinjianKpi jinjianKpi) {
        return R.status(jinjianKpiService.save(jinjianKpi));
    }


    /**
     * 删除员工业绩
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianKpi:remove')")
    @Log(title = "员工业绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(jinjianKpiService.deleteJinjianKpiByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianKpi:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(jinjianKpiService.findMaxSort(new JinjianKpi()));
    }

    /**
     * 导出员工业绩列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianKpi:export')")
    @Log(title = "员工业绩", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(JinjianKpi jinjianKpi) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            jinjianKpi.setCompanyId(null);
        } else {
            jinjianKpi.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<JinjianKpi> list = jinjianKpiService.findList(jinjianKpi);
        ExcelUtil<JinjianKpi> util = new ExcelUtil<JinjianKpi>(JinjianKpi.class);
        return util.exportExcel(list, "员工业绩数据");
    }


    /**
     * 查询员工业绩列表
     */
    @GetMapping("/getSelfKpiStatisticVo")
    public R<PageInfo> getSelfKpiStatisticVo(JinjianKpi jinjianKpi, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            jinjianKpi.setCompanyId(null);
        } else {
            jinjianKpi.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        jinjianKpi.setPage(new PageDomain(request, response));
        return R.data(jinjianKpiService.getSelfKpiStatisticVo(jinjianKpi));
    }


    /**
     * 员工业绩合计
     */
    @GetMapping("/getSumKpiStatisticVo")
    public R getSumKpiStatisticVo(JinjianKpi jinjianKpi) {
        return R.data(jinjianKpiService.getSumKpiStatisticVo(jinjianKpi));
    }
}
